// pages/my/vip/vip_grade/index.js

const app=getApp();
const payAndInput=require("../../../controllers/ControllerVip").controller.payAndInput;
const amountHistory=require("../../../controllers/ControllerVip").controller.amountHistory;
const Controllers = require('../../../controllers/Controller').controller;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    globalStype:'',
    moneyData:{},
    amountList:[],
    outList:[],
    value:{
      genre:0,
      shopId:"",
      amountType:0,
      pageNumber:1,
      lowLine:'',
      types:"0,1,2,3,4,5,6,7,9"
    },
    tabItem:0,
    selectIndex:0,
    lastPage:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (option) {
    this.setData({globalStype:app.globalData.globalStype})
    if(option.shopId){
      this.setData({
        shopId:option.shopId
      })
      this.setData({
        'value.shopId':option.shopId,
      })
      wx.setStorageSync('shopId', option.shopId)
    }else{
      this.setData({
        'value.shopId':app.globalData.shopId,
      })
    }
    if(wx.getStorageSync('shopId')){
      this.setData({
        shopId:wx.getStorageSync('shopId')
      })
    }
    if(option.invitationAccountId){
      this.setData({
        invitationAccountId:option.invitationAccountId
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (wx.getStorageSync('usertoken')) {
      //加载首页数据
      this.myInformation()
      if(this.data.invitationAccountId){
        this.invitationPerson()
      }
      this.getPayAndInput()
      this.getAmountHistory()
    }
      //无usertoken，等待login登录回调
      app.usertokenReadyCallback = res => {
        if(res.data.sessionId){
          this.myInformation()
          if(this.data.invitationAccountId){
            this.invitationPerson()
          }
          this.getPayAndInput()
          this.getAmountHistory()
        }
      }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getIncomeOrCost()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (e) {
    let obj = {
      // title: this.data.shopDetailsdata.shopName,
      path:'/pages/my/vip/vip_ surplus/index?shopId=' + this.data.shopId+"&invitationAccountId="+this.data.myInformationdata.accountId,
      success: function (res) {
        
      },
      fail(err) {
        
      }
    }
    return obj
  },
  // 进行tab跳转选项
  selectTab(e){
    let index=e.currentTarget.dataset.index
    this.setData({
      selectIndex:index
    })
    if(index==0){
      this.setData({
        "value.genre":0,
        "value.pageNumber":1,
        "value.lowLine":'',
      })
    }else if(index==1){
      this.setData({
        "value.genre":1,
        "value.pageNumber":1,
        "value.lowLine":0,
      })
    }else if(index==2){
      this.setData({
        "value.genre":1,
        "value.pageNumber":1,
        "value.lowLine":1,
      })
    }
    this.getAmountHistory()
  },

  //获取余额

  getPayAndInput(){
    let value={
      amountType:0,
      shopId:this.data.value.shopId
    }
    payAndInput(value).then(res=>{
      this.setData({
        moneyData:res.data
      })
    })
  },

  //获取第一页充值消费记录
  getAmountHistory(){
    let value=this.data.value
    // let value2=this.data.value2
    amountHistory(value).then(res=>{
      this.setData({
        amountList:res.data.list,
        lastPage:res.data.lastPage
      })
    })
  },

  //下滑获取充值或者消费的记录
  getIncomeOrCost(){
    if(this.data.lastPage==false){
      let value=this.data.value
      value.pageNumber=value.pageNumber+1
      this.setData({
        value:value
      })
      amountHistory(value).then(res=>{
        let list1=this.data.amountList;
        let list2=res.data.list;
        let cList=list1.concat(list2);

        this.setData({
          amountList:cList,
          lastPage:res.data.lastPage
        })
      })
    }
  },
  
  //跳转充值
  jumpRecharge(){
    wx.navigateTo({
      url: '/pages/my/vip/vip_renew/index',
    })
  },

  //返回首页
  backIndex(){
    let page=getCurrentPages()
    let shopId=this.data.shopId
    console.log(page.length)
    if(page.length<=1){
      wx.redirectTo({
        url: '/pages/Index/Index?shopId='+shopId,
      })
    }
  },

  myInformation(){
    //获取数据
    Controllers.myInformation({shopId:this.data.shopId}).then(res => {
      this.setData({
        myInformationdata:res.data.accountRelation
      })
      app.globalData.myshopinfo=res.data.accountRelation
    })
  },

   //绑定上级
   invitationPerson(){
    let value={}
    value.invitationAccountId=this.data.invitationAccountId
    value.shopId=wx.getStorageSync('shopId')
    Controllers.invitationPerson(value).then(res=>{
        
    })
  },

  //跳转详情
  toDetail(e){

    let orderNo=e.currentTarget.dataset.orderno
    let genre=e.currentTarget.dataset.genre
    let type=e.currentTarget.dataset.type
    let name=e.currentTarget.dataset.name
    let head=e.currentTarget.dataset.head
  
    wx.navigateTo({
      url: '/pages/my/vip/vip_ surplus/billDetail?orderNo='+orderNo+"&genre="+genre+"&type="+type+"&name="+name+"&head="+head,
    })
  }
})